<template>
<div class="envelope views-container">
    <share-link :config="shareLinkConfig" ref="sharelink">
        <template slot="title">-活动</template>
        <template slot="el-form-item">
            <el-form-item label="页面链接" >
                <el-input style="width: 270px;" readonly placeholder="请输入内容" v-model="shareLinkConfig.params.qrUrl" class="input-with-select">
                    <el-button slot="append" @click="handleClipboard(shareLinkConfig.params.qrUrl,$event)">复制</el-button>
                </el-input>
            </el-form-item>
            <el-form-item label="直接弹出购买界面的链接">
                <el-input style="width: 270px;" readonly placeholder="请输入内容" v-model="shareLinkConfig.params.linkUrl" class="input-with-select">
                    <el-button slot="append" @click="handleClipboard(shareLinkConfig.params.linkUrl,$event)">复制</el-button>
                </el-input>
            </el-form-item>
        </template>
    </share-link>
    <div class="wlm-table">
        <div class="wlm-table-hearder-btn">
            <router-link tag="span" to="/application/envelopeAdd">
                <el-button size="small" type="primary">添加活动</el-button>
            </router-link>
        </div>
        <div class="wlm-table-header">
            <el-form :model="tableFormatData.envelope.files" size="small" label-width="100px" class="retail-form" label-position="right" @submit.native.prevent>
                <el-form-item label="" class="filter">
                    <el-form-item label="活动名称：" class="search">
                        <el-input @keyup.enter.native="filesSerch" v-model="tableFormatData.envelope.files.name" placeholder="请输入活动名称" style="width:160px;"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="活动类型：" class="search">
                        <el-select v-model="tableFormatData.envelope.files.couponType" placeholder="请选择">
                            <el-option v-for="item in [{lable:'全部',value:'0'},{lable:'满减券',value:'10'},{lable:'折扣券',value:'20'},{lable:'运费券',value:'30'},{lable:'随机券',value:'40'}]" :key="item.value" :label="item.lable" :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item> -->
                </el-form-item>
                <el-form-item label="" class="filter-btns">
                    <el-button type="primary" @click="filesSerch">筛选</el-button>
                    <el-button type="text" @click="filesEmpty">清空筛选条件</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="wlm-table-content">
            <el-tabs v-model="tableFormatData.envelope.files.status" type="card" @tab-click="filesSerch">
                <el-tab-pane label="全部活动" name="0"></el-tab-pane>
                <el-tab-pane label="未开始" name="1"></el-tab-pane>
                <el-tab-pane label="进行中" name="2"></el-tab-pane>
                <el-tab-pane label="已结束" name="3"></el-tab-pane>
                <el-tab-pane label="暂停" name="4"></el-tab-pane>
            </el-tabs>
            <el-table :ref="tableFormatData.envelope.key" :data="tableFormatData.envelope.tableData" @selection-change="handleSelectionChange" style="width: 100%">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column label="活动名称" >
                    <template slot-scope="scope">
                        <el-popover trigger="hover" placement="top">
                            <p>{{scope.row.name}}</p>
                            <div slot="reference" class="wlm-table-wrapper">
                                <p>{{scope.row.name}}</p>
                            </div>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column label="活动内容" >
                    <template slot-scope="scope">
                        <span>{{scope.row.share_content}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="活动状态" >
                    <template slot-scope="scope">
                        <span>{{status_type[`${scope.row.status}`]}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="进行中" >
                    <template slot-scope="scope">
                        <span>{{scope.row.conduct_num}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="领取成功" >
                    <template slot-scope="scope">
                        <span>{{scope.row.success_num}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="剩余库存" >
                    <template slot-scope="scope">
                        <span>{{scope.row.total_num}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="已使用" >
                    <template slot-scope="scope">
                        <span>{{scope.row.is_use}}</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="120" label="操作"  width="290">
                    <template slot-scope="scope">
                        <div class="operation-group">
                            <el-button v-if="scope.row.status == 2" class="wlm-text" type="text" @click="changeTableItem({id:scope.row.id,status:'4'},tableFormatData.envelope.change.stop)">停止</el-button>
                            <el-button v-if="scope.row.status == 4" class="wlm-text text-danger" type="text" @click="changeTableItem({id:scope.row.id},tableFormatData.envelope.change.stop)">恢复</el-button>
                            <div class="btn-line" v-if="scope.row.status==2||scope.row.status==4"></div>
                            <router-link class="wlm-text" tag="span" :to="{path:'/application/envelopeAdd', query :{ id:scope.row.id}}">
                                <el-button type="text">{{scope.row.status==3?'查看':'编辑'}}</el-button>
                            </router-link>
                            <div class="btn-line" v-if="scope.row.status==1||scope.row.status==2"></div>
                            <el-button class="wlm-text" type="text" v-if="scope.row.status==1||scope.row.status==2" @click="sharelink(scope.row.id)">推广</el-button>
                            <div class="btn-line" v-if="scope.row.status==1||scope.row.status==3"></div>
                            <el-button class="wlm-text" type="text" v-if="scope.row.status==1||scope.row.status==3" @click="delTableItem(scope.row.id)">删除</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-content flex-row flex-justify-b flex-align-c">
                <el-row class="pagination-btns">
                    <el-checkbox class="check-all" true-label="1" false-label="0" v-model="tableFormatData.envelope.files.checkall" @change="toggleSelection">全部</el-checkbox>
                </el-row>
                <el-pagination :disabled="!hasTableData" @size-change="listPageChange" @current-change="listPageChange" :current-page.sync="tableFormatData.envelope.pagination.page" :page-sizes="tableFormatData.envelope.pagination.pagesizes" :page-size.sync="tableFormatData.envelope.pagination.list_rows" layout="total, sizes, prev, pager, next, jumper" :total="hasTableData?tableFormatData.envelope.pagination.total : 0">
                </el-pagination>
            </div>
        </div>
    </div>
</div>
</template>

<script>
import {
  getRedEnvelopesList,
  deleteRedEnvelopes,
  editRedEnvelopesStatus,
  redEnvelopesExtension
} from '@/api/application'
import mixins from '@/mixins/mixins'
import ShareLink from '@/components/ShareLink/index'
export default {
  mixins: [mixins.getters('Table')],
  name: 'Envelope',
  components: {
    ShareLink
  },
  filters: {
    numFilter(value) {
      const tempVal = parseFloat(value).toFixed(3)
      const realVal = tempVal.substring(0, tempVal.length - 1)
      return realVal
    }
  },
  data() {
    return {
      dialogTableVisible: false,
      filters: {
        addZero: function(data) {
          return data.toFixed(2)
        }
      },
      shareLinkConfig: {
        current: 'weChatQr',
        params: {
          weChatQr: '',
          appQr: ''
        }
      },
      status_type: {
        1: '未开始',
        2: '进行中',
        3: '已结束',
        4: '暂停'
      },
      tableFormatData: {
        current: 'envelope',
        envelope: {
          key: 'envelope',
          api: {
            getList: getRedEnvelopesList,
            delList: deleteRedEnvelopes
          },
          tableData: [],
          files: {
            ids: [],
            name: '',
            checkall: '0',
            Recycle: '1',
            redirect: 'id'
          },
          change: {
            stop: {
              name: '停止',
              key: 'stop',
              api: editRedEnvelopesStatus
            }
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  methods: {
    handleSelect(key, keyPath) {
      this.$refs.carousel.setActiveItem(key)
    },
    sharelink(id) {
      redEnvelopesExtension({ id: id, op: 'tg' }).then((response) => {
        const { data: { data: { wxapp, wechat } } } = response
        this.shareLinkConfig.params.weChatQr = wechat
        this.shareLinkConfig.params.appQr = wxapp
        this.shareLinkConfig.current = wechat ? 'weChatQr' : wxapp ? 'appQr' : ''
      })
      this.shareLinkConfig.params.id = id
      this.$refs.sharelink.toggle()
    }
  }
}
</script>

<style lang="scss" scoped>
.envelope {
    font-size: 14px;
}
</style>
